//index.js
const UTIL = require('../../utils/util.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    shouldAccept: {
      name: '注册代表同意',
      value: '注册代表同意',
      checked: 'false'
    },
    disableCreate: true,
    iptOne: false,
    iptTwo: false,
    iptThree: true,
    email: '',
    password: '',
    rePassword: '',
    userInfo: {}
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function () {
    console.log('onLoad')
    var that = this;
    let yxInfo = UTIL.Login.getYxInfo();
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    });

    that.setData({
      email: (yxInfo && yxInfo.email) || '',
      password: (yxInfo && yxInfo.password) || '',
      rePasswprd: (yxInfo && yxInfo.rePassword) || ''
    });
  },

  onShareAppMessage: function() {
    return {
      title: "游侠注册，就找最会游，专业、认真、靠谱",
      path: "/pages/index/index"
    }
  },

  gotoTreaty: function() {
    wx.navigateTo({
      url: '../treaty/treaty',
    })
  },

  agreeCheckboxChange: function(e) {
    if(e && e.detail && e.detail.value && Array.isArray(e.detail.value) && e.detail.value.length > 0) {
      this.setData({
        iptThree: true
      });
    }else {
      this.setData({
        iptThree: false
      });
      wx.showModal({
        title: '不同意本协议，将无法注册游侠',
        showCancel: false
      })
    }
    this.canYxCreate();
  },

  emailBlur: function(e) {
    if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(e.detail.value)) {
      this.setData({
        iptOne: false
      });
      wx.showToast({
        title: '邮箱未填写或格式错误',
        duration: 1000,
        icon: ''
      });
    }else {
      this.setData({
        iptOne: true
      });
    }
    this.canYxCreate();
  },

  emailInput: function(e) {
    let value = e.detail.value;
    if (value && value.trim() != '') {
      this.setData({
        email: e.detail.value
      });
    } else {
      wx.showToast({
        title: '请填写邮箱',
        duration: 1000,
        icon: ''
      });
    }
  },

  passwordInput: function(e) {
    let value = e.detail.value;
    if (value && value.trim() != '') {
      this.setData({
        password: e.detail.value
      });
    } else {
      wx.showToast({
        title: '请填写密码',
        duration: 1000,
        icon: ''
      });
    }
  },

  rePasswordInput: function(e) {
    let value = e.detail.value;
    if (value && value.trim() != '') {
      this.setData({
        rePassword: e.detail.value
      });
    } else {
      wx.showToast({
        title: '请重填密码',
        duration: 1000,
        icon: ''
      });
    }
  },

  rePasswordBlur: function(e) {
    if(this.data.password.trim() != this.data.rePassword.trim()) {
      this.setData({
        iptTwo: false
      });
      wx.showToast({
        title: '两次输入的密码不一致',
        duration: 1000,
        icon: ''
      });
    }else {
      this.setData({
        iptTwo: true
      });
    }
    this.canYxCreate();
  },

  canYxCreate: function() {
    if(this.data.iptOne && this.data.iptTwo && this.data.iptThree) {
      this.setData({
        disableCreate: false
      });
    }else {
      this.setData({
        disableCreate: true
      });
    }
  },

  register: function() {
    UTIL.Login.setYxInfo({
      email: this.data.email,
      password: this.data.password,
      rePassword: this.data.rePassword
    });
    wx.navigateTo({
      url: '../baseinfo/baseinfo?step=1',
    });
  }
})
